class Node {
    constructor(data) {
        this.data = data
        this.next = null
        this.prev = null
    }
}

class DoubleCircleLinkList {
    size = 0
    head = null
    tail = null


    append(data) {
        const node = new Node(data)
        if (this.size === 0) { // 直接添加
            this.head = node
            this.tail = node
            this.head.next = this.tail
            this.tail.prev = this.head
        } else {
            this.tail.next = node
            node.prev = this.tail
            node.next = this.head
            this.tail = node
        }
        this.size++
    }

    insertIndex(index, data) {
        const node = new Node(data)
        if (index < 0 || index > this.size) {
            return 
        }
        if (this.size === 0) { // 直接添加 
            this.head = node
            this.tail = node
            this.head.next = this.tail
            this.tail.prev = this.head
        }
        if (index === 0 || index === 1) {
            this.head.prev = node
            this.tail.next = node
            node.prev = this.tail
            node.next = this.head
            this.head = node
        }
        if (index > 1 && index <= this.size) { // 在中间位置,如果是在最后一个结点的位置添加，也不会影响结果，this.tail 始终指向最后一个结点
            let current = this.head
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            previous.next = node
            node.prev = previous
            node.next = current
            current.prev = node
        }
        this.size++
        return true
    }

    delIndex(index) {
        if (index <= 0 || index > this.size) {
            return false
        }
        if (index === 1) {
            this.tail.next = this.head.next
            this.head.next.prev = this.tail
            this.head = this.head.next
        }
        if (index === this.size) {
            this.tail.prev.next = this.head
            this.head.prev = this.tail.prev
            this.tail = this.tail.prev
        }
        if (index > 1 && index < this.size) {
            let current = this.head
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            previous.next = current.next
            current.next.prev = previous
        }
        this.size--
        return true
    }

    getSize() {
        return this.size
    }

    // 遍历
    print(){
        if (this.size === 0) {
            console.log("这是个空链表");
            return
        }
        let node = this.head
        while (node) {
            console.log(node.data);
            if (node === this.tail) {
                break
            } else {
                node = node.next
            }
        }
    }
}

const doubleCircleLink = new DoubleCircleLinkList()

doubleCircleLink.append(1)
doubleCircleLink.append(2)
doubleCircleLink.append(3)
doubleCircleLink.print()
console.log("链表长度为：", doubleCircleLink.getSize());

doubleCircleLink.insertIndex(2, 4)
doubleCircleLink.print()
console.log("链表长度为：", doubleCircleLink.getSize());

doubleCircleLink.insertIndex(0,10)
doubleCircleLink.print()

console.log("链表长度为：", doubleCircleLink.getSize());

doubleCircleLink.delIndex(5)
doubleCircleLink.print()
console.log("链表长度为：", doubleCircleLink.getSize());

doubleCircleLink.delIndex(4)
doubleCircleLink.print()
